package com.tide.crm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.tide.crm.dao.ConvergeDictDao;
import com.tide.crm.dto.ConvergeDictDto;
import com.tide.crm.dto.ResponseInfo;
import com.tide.crm.model.ConvergeDict;
import com.tide.crm.page.table.PageTableHandler;
import com.tide.crm.page.table.PageTableRequest;
import com.tide.crm.page.table.PageTableResponse;
import com.tide.crm.service.ConvergeDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tide.crm.page.table.PageTableHandler.CountHandler;
import com.tide.crm.page.table.PageTableHandler.ListHandler;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/convergeDict")
public class ConvergeDictController {

    @Autowired
    private ConvergeDictDao convergeDictDao;

    @Resource(name="convergeDictService")
    private ConvergeDictService convergeDictService;
    
    @PostMapping
    @ApiOperation(value = "保存")
    public ConvergeDict save(@RequestBody ConvergeDict convergeDict) {
        convergeDictDao.save(convergeDict);

        return convergeDict;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public ConvergeDict get(@PathVariable Long id) {
        return convergeDictDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public ConvergeDict update(@RequestBody ConvergeDict convergeDict) {
        convergeDictDao.update(convergeDict);

        return convergeDict;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return convergeDictDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<ConvergeDict> list(PageTableRequest request) {
                return convergeDictDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        convergeDictDao.delete(id);
    }
    
   


    @PostMapping(value="findDictListForQuery")
    @ApiOperation(value = "用于查询条件的Item")
    public ResponseInfo findItemListForQuery(@RequestBody ConvergeDictDto convergeDictDto) {
        ResponseInfo responseInfo = new ResponseInfo();
    	if(null == convergeDictDto.getDictCodes()) {
    		convergeDictDto.setDictCodes("weightvalue,quantitative");
    	}
    	String codes = convergeDictDto.getDictCodes();
    	
    	String dictCode[] = codes.split(",");
    	
    	List<String> codeList = new ArrayList<String>();
    	
    	for(String code : dictCode) {
    		codeList.add(code);
    	}
    	
    	Map<String,Object> params = new HashMap<String,Object>();
    	params.put("dictCodeList", codeList);
    	List<Map<String,Object>> resultList = convergeDictService.findDictListForQuery(params);
    	responseInfo.setData(resultList);
        return responseInfo;
    }
}
